home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
pcr
/
pcr4_4.lha
/
DIST
/
loading
/
transform_symtab_sort.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-05-30
|
5KB
|
195 lines
/* File: transform_symtab_sort.c - last edit:
* lhowe Wed Nov 1 09:59:31 1989
* horton Wed Mar 21 12:13:44 1990 (merged May 30, 1991)
* Boehm Th Mar 21 1991
*
* Copyright (C) 1989 by Xerox Corporation. All rights reserved.
* Copyright (c) 1989 by Xerox Corporation. All rights reserved. */
#include <errno.h>
#include <stdio.h>
#include <sys/param.h>
/* This program takes a short ILsymtab.pidXXX file and sorts it by the address
* which is the first numeric field in a line. The address is first converted
* from hexadecimal to base 10. Then qsort is called to do the sort.
*/
/* This handles input format errors very poorly, since there doesn't seem to
* be an easy way to tell scanf to stop at the end of a line. Error messages
* are nearly guaranteed to be wrong. But they now contain line numbers,
* which should be correct. - HB 3/21/91
*/
#define MAXENTRIES 1024 /* maximum number of loaded modules we can handle */
#define FILENAME "F:"
#define NODEBUG "f:"
#define COMMON "C:"
#define FALSE 0
#define TRUE 1
static int address_compare();
static void selectModules();
static void putModules();
/*
* Other stuff
*/
static long num_entries = 0; /* actual number of loaded modules in this file# */
static FILE *file;
/* 'struct entry' keeps track of information about each loaded module */
struct entry
{
unsigned addr_key;
char field1[4];
char field2[12];
char field3[12];
char field4[12];
char field5[12];
char *filename;
} entries[MAXENTRIES];
char *modules[MAXENTRIES];
sort_main()
{
file = stdin;
if (getLoadedModules())
exit(1);
/******************************************************************
* Write out the sorted list of loaded modules.
******************************************************************/
file = stdout;
putModules();
}
/***********************************************************************
* Function: getLoadedModules
*
* read in the info from the info file and then we can perform
* necessary operations on it. Returns 0 on success
**********************************************************************/
int getLoadedModules()
{
int result;
register int i;
/* Read the names of the modules that have been loaded into pcr */
i = 0;
while ((result = fscanf(file, "%3s", entries[i].field1)) != EOF)
{
if (result != 1)
{
fprintf(stderr, "Cannot read from stdin, wrong format\n");
return (1);
}
entries[i].filename = (char *) malloc(MAXPATHLEN);
if (entries[i].filename == 0) {
perror("Sort: getLoadedModules: malloc failed");
exit(1);
}
if ((strcmp(entries[i].field1, FILENAME) == 0) ||
(strcmp(entries[i].field1, NODEBUG) == 0))
{
/* This is a file name line. */
result = fscanf(file, "%11s %11s %11s %11s %199s\n",
entries[i].field2, entries[i].field3,
entries[i].field4, entries[i].field5,
entries[i].filename);
if (result != 5 || result == EOF)
{
fprintf(stderr, "ERROR: file line %d has wrong format\n",i+1);
return (1);
}
}
else if (strcmp(entries[i].field1, COMMON) == 0)
{
/* This is a common symbol line. */
result = fscanf(file, "%11s %199s\n", entries[i].field2,
entries[i].filename);
if (result != 2 || result == EOF)
{
fprintf(stderr,
"ERROR: common symbol line %d has wrong format\n",
i+1);
return (1);
}
}
else
{
fprintf(stderr,
"ERROR: lines must start with either \"F:\" or \"C:\"");
fprintf(stderr, " Line %d started with \"%s\"\n",
i+1, entries[i].field1);
fprintf(stderr, "(may be format error on line %d)\n", i);
return (1);
}
/* convert the address field from a hexadecimal to a long integer */
entries[i].addr_key = strtol(entries[i].field2, (char **) NULL, 16);
i++;
}
num_entries = i;
qsort(entries, num_entries, sizeof(struct entry), address_compare);
return (0); /* success */
}/* end getLoadedModules() */
static int address_compare(i, j)
unsigned *i, *j;
{
return (*i - *j);
}
/***********************************************************************
* Function: putModules
**********************************************************************/
static void putModules()
{
int i;
for (i = 0; i < num_entries; i++)
{
if ((strcmp(entries[i].field1, FILENAME) == 0) || (strcmp(entries[i].field1, NODEBUG) == 0))
{
/* Write out the file line. */
/* Keep all the symbols that we have for this file. */
if (fprintf(file, "%s %s %s %s %s %s\n",
entries[i].field1, entries[i].field2,
entries[i].field3, entries[i].field4,
entries[i].field5,
entries[i].filename) == EOF)
{
fprintf(stderr, "Cannot write to stdout, errno = %d\n", errno);
return;
}
}
else
{
/* Write out the common symbol line. */
if (fprintf(file, "%s %s %s\n",
entries[i].field1, entries[i].field2,
entries[i].filename) == EOF)
{
fprintf(stderr, "Cannot write to stdout, errno = %d\n", errno);
return;
}
}
free(entries[i].filename);
}
}
/* Revision History
*
* lah 11/1/89 Created from Michael Tao's 10/31/89 version of
* selectILsymtab.c.
*/